package com.bicon.botu.core.base.config;

import com.bicon.botu.core.base.common.BusinessException;
import com.bicon.botu.core.base.common.Constants;
import com.bicon.botu.core.base.common.entity.BaseResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 全局异常处理
 *
 * @author tandk
 * @date 2017/6/12 0012 下午 4:27
 */
@ControllerAdvice
@Slf4j
@ResponseBody
public class GlobalExceptionHandler {
    @ExceptionHandler(value = Exception.class)
    public Object exceptionHandler(HttpServletRequest request, Exception exception) throws Exception {
        if(exception instanceof BusinessException){
            log.info("【业务异常】", exception);
            return new BaseResult(((BusinessException) exception).getErrorCode(), exception.getMessage());
        }else{
            log.error("【系统异常】", exception);
            return new BaseResult(Constants.RESPONSE_CODE_500, Constants.RESPONSE_MESSAGE_500, exception.toString());
        }
    }
}
